﻿using System;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class Order : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("SA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }

                if (Request.QueryString["pnr"] == null)
                {
                    if (Session["BK_ID"] != null)
                    {
                        txtOrderNo.Text = Session["BK_ID"].ToString();
                        lbtnQuery_Click(sender, e);
                    }
                }
                else
                {
                    txtOrderNo.Text = Request.QueryString["pnr"];
                    lbtnQuery_Click(sender, e);
                }
                try
                {
                    lstCurrency.SelectedValue = Session["DEFAULT_CURRENCY"].ToString();
                }
                catch (Exception ex)
                {
                    //Does nothing!
                }
            }


            //btnReceipts.Attributes.Add("onclick", "window.open('receipts2.aspx?bk_id=" + hidOrderNo.Value+"');");
            btnCommit.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnCancel.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnPayBefore.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnAddNote.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            butK_Seats.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnCancelSeats.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");

            lblError.Text = "";
            lblErrorK.Text = "";
            lblPayError.Text = "";
            lblEmdError.Text = "";
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            hidOrderNo.Value = txtOrderNo.Text;
            hidPayOrderNo.Value = hidOrderNo.Value;
            hidOrderNoEmd.Value = hidOrderNo.Value;
            Session["BK_ID"] = hidOrderNo.Value;
            DetailsView1.DataBind();
            GridView1.DataBind();
            GridView2.DataBind();
            btnReceipts.Attributes.Add("onclick", "javascript:window.open('Itinerary.aspx?bk_id=" + hidOrderNo.Value + "');");
        }

        protected void btnEditNames_Click(object sender, EventArgs e)
        {
            //Session["BK_ID"] = txtOrderNo.Text;
            Response.Redirect("OrderPassengers.aspx");
        }

        protected void btnCommit_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "bll_bk_submit";
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "Order committed.";
                        break;
                    case 1:
                        lblError.Text = "Order not found!";
                        break;
                    case 2:
                        lblError.Text = "Commition failed!";
                        break;
                    case 3:
                        lblError.Text = "Order already committed!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "bll_bk_cancel";
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "Order Cancelled.";
                        GridView1.DataBind();
                        DetailsView1.DataBind();
                        break;
                    case 1:
                        lblError.Text = "Updating cabin failed!";
                        break;
                    case 2:
                        lblError.Text = "Updating class failed!";
                        break;
                    case 3:
                        lblError.Text = "Releasing blocked cabin failed!";
                        break;
                    case 4:
                        lblError.Text = "Releasing blocked class failed!";
                        break;
                    case 5:
                        lblError.Text = "Can not cancel order in current status, or order not found!";
                        break;
                    case 6:
                        lblError.Text = "Check authority!";
                        break;
                    case 81:
                        lblError.Text = "Cancelling order root failed!";
                        break;
                    case 90:
                        lblError.Text = "Cancelling order trips failed!";
                        break;
                    case 150:
                        lblError.Text = "Re-Calculate nested class avail failed!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void butK_Seats_Click(object sender, EventArgs e)
        {
            //if (Request.Form["KEY"] == null)
            //{
            //    lblErrorK.Text = "Please select a trip to K!";
            //    return;
            //}
            if(lstAction.SelectedValue=="K")
            {
                if (Session["ACL"].ToString().IndexOf("SA6") < 0)
                {
                    lblErrorK.Text = "Unauthorized!";
                    return;
                }
            }
            else if (lstAction.SelectedValue == "B")
            {
                if (Session["ACL"].ToString().IndexOf("SA7") < 0)
                {
                    lblErrorK.Text = "Unauthorized!";
                    return;
                }
            }

            int trip_sn;
            Int64 bk_id;
            try
            {
                //trip_sn = Convert.ToInt16(Request.Form["KEY"].ToString());
                trip_sn = Convert.ToInt16(lstTripSn2.SelectedValue);
                bk_id = Convert.ToInt64(hidOrderNo.Value);
                K_Seats(bk_id, trip_sn, /*pay_before,*/lstAction.SelectedValue);
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrorK.Text = ex.Message;
                else
                    lblErrorK.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void K_Seats(Int64 bk_id, int trip_sn, /*DateTime pay_before,*/ string action)
        {
            if (Session["ACL"].ToString().IndexOf("AL2") < 0)
            {
                lblErrorK.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_bk_confirm_seats";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", bk_id);
            para = cmd.Parameters.AddWithValue("@trip_sn", trip_sn);
            para = cmd.Parameters.AddWithValue("@action", action);
            para = cmd.Parameters.AddWithValue("@target_cabin", lstCabin.SelectedValue);
            para = cmd.Parameters.AddWithValue("@target_class", lstClass.SelectedValue);
            //para = cmd.Parameters.AddWithValue("@pay_before", pay_before);
            para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            ret_code = TinyDAL.Exec(ref cmd);
            switch (ret_code)
            {
                case 0:
                    lblErrorK.Text = "Done!";
                    GridView1.DataBind();
                    break;
                case 1:
                    lblErrorK.Text = "Order or trip not found!";
                    break;
                case 2:
                    lblErrorK.Text = "Flight not open or expired!";
                    break;
                case 3:
                    lblErrorK.Text = "Cabin not found!";
                    break;
                case 5:
                    lblErrorK.Text = "Class blocked!";
                    break;
                case 4:
                    lblErrorK.Text = "Class not found or wrong market airline!";
                    break;
                case 6:
                    lblErrorK.Text = "Insufficient cabin!";
                    break;
                case 7:
                    lblErrorK.Text = "Insufficient class!";
                    break;
                case 8:
                    lblErrorK.Text = "Insufficient seats!";
                    break;
                case 9:
                    lblErrorK.Text = "Illegal airline!";
                    break;
                case 10:
                    lblErrorK.Text = "Virtual flight:Insufficient cabin!";
                    break;
                case 11:
                    lblErrorK.Text = "Virtual flight:Insufficient class!";
                    break;
                case 12:
                    lblErrorK.Text = "Can not change cabin/class!";
                    break;
                case 101:
                    lblErrorK.Text = "Block class failed!";
                    break;
                case 102:
                    lblErrorK.Text = "Block cabin failed!";
                    break;
                case 103:
                    lblErrorK.Text = "Update order trip status failed!";
                    break;
                case 104:
                    lblErrorK.Text = "Update order payment deadline failed!";
                    break;
                case 111:
                    lblErrorK.Text = "K class failed!";
                    break;
                case 112:
                    lblErrorK.Text = "K cabin failed!";
                    break;
                case 113:
                    lblErrorK.Text = "K order seats failed!";
                    break;
                case 150:
                    lblErrorK.Text = "Calculate nested available failed!";
                    break;
                case 201:
                    lblError.Text = "201:Class not found!";
                    break;
                case 202:
                    lblError.Text = "202:Insufficient nested seats";
                    break;
                case 203:
                    lblError.Text = "203:Nesting class seats failed!";
                    break;
                case 204:
                    lblError.Text = "204:Nesting class seats failed!";
                    break;
                default:
                    lblErrorK.Text = "Error:" + ret_code.ToString();
                    break;
            }

        }
        protected void dsCommon_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            //检查订单状态，是否允许提交/撤销/支付/改名等
        }
        protected void btnCancelSeats_Click(object sender, EventArgs e)
        {
            //支付前允许取消？取消一段等于取消整个订单？多航空公司执行的订单是否任一段承运人都可取消订单？
            //任一段座位没有ready，均无法支付！？除非Open状态
            //已支付的以票为准，不能取消！
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblErrorK.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_bk_cancel_trip";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
            para = cmd.Parameters.AddWithValue("@trip_sn", lstTripSn2.SelectedValue);//Request.Form["KEY"].ToString());
            para = cmd.Parameters.AddWithValue("@action", lstCancelTypes.SelectedValue);
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblErrorK.Text = "Done!";
                        GridView1.DataBind();
                        break;
                    case 1:
                        lblErrorK.Text = "Order or trip not found!";
                        break;
                    case 2:
                        lblErrorK.Text = "Update trip failed!";
                        break;
                    case 3:
                        lblErrorK.Text = "Order in waiting queue can not be timeout!";
                        break;
                    case 4:
                        lblErrorK.Text = "Update flt cabin failed!";
                        break;
                    case 5:
                        lblErrorK.Text = "Update flt class failed!";
                        break;
                    case 150:
                        lblErrorK.Text = "Re-calculate class failed!";
                        break;
                    default:
                        lblErrorK.Text = "Unknow error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrorK.Text = ex.Message;
                else
                    lblErrorK.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        /*
        protected void btnReceipts_Click(object sender, EventArgs e)
        {
            //Response.Redirect("receipts2.aspx?bk_id=" + hidOrderNo.Value);

            //string showOpen = "<script language=´javascript´>window.open(´receipts2.aspx?bk_id=" + hidOrderNo.Value + "')</script>";
            //if (!IsClientScriptBlockRegistered("clientScript"))
            //    RegisterClientScriptBlock("clientScript", showOpen);

            Page.ClientScript.RegisterClientScriptBlock(GetType(), "openNew", "<script language='javascript'>window.open('receipts.aspx?bk_id=" + hidOrderNo.Value + "')</script>"); 

        }*/
        protected void btnLogs_Click(object sender, EventArgs e)
        {
            Response.Redirect("OrderLog.aspx?pnr=" + txtOrderNo.Text);//可查已不在系统中的订单日志！
        }

        protected void btnPayBefore_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA8") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            try
            {
                DateTime.Parse(txtPayBefore2.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_bk_set_payment_deadline";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
            para = cmd.Parameters.AddWithValue("@pay_before", txtPayBefore2.Text);
            para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "Done!";
                        DetailsView1.DataBind();
                        break;
                    case 1:
                        lblError.Text = "Failed! Can not edit in current status, or order not found!";
                        break;
                    default:
                        lblError.Text = "Unknow error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnAddNote_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                txtNote.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_bk_add_note";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
            para = cmd.Parameters.AddWithValue("@note", txtNote.Text);
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        txtNote.Text = "";
                        GridView3.DataBind();
                        break;
                    case 1:
                        txtNote.Text = "*Failed:" + txtNote.Text;
                        break;
                    default:
                        txtNote.Text = "*Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    txtNote.Text = ex.Message;
                else
                    txtNote.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnRefreshNotes_Click(object sender, EventArgs e)
        {
            GridView3.DataBind();
        }
        protected void btnPayQuery_Click(object sender, EventArgs e)
        {
            //“支付中”的订单才可发起查询！
            //if (Session["ACL"].ToString().IndexOf("BD1") < 0)
            //{
            //    lblError.Text = "Unauthorized!";
            //    return;
            //}

            //StringBuilder str = new StringBuilder();
            //str.Append("?order=" + hidOrderNo.Value);
            //str.Append("&uid=" + Session["USER"].ToString());
            //string query = str.ToString();
            //str.Append("&pwd=" + Session["PWD"].ToString());
            //string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str.ToString(), "SHA1");
            //Response.Redirect(Application["pay_query"].ToString() + query + "&sign=" + sign);

            //发出查询请求后，支付网关反馈到后台，如果成功更新支付状态，用户手工刷新查看订单状态是否变化！
        }
        protected void btnPay_Click(object sender, EventArgs e)
        {
            //离线支付，交易额不转入本公司帐号。用于连接支付网关前的测试。
            //if (lblTotal.Text == "?" || lblTotal.Text == "")
            //{
            //    lblError.Text = "Please calculate total fare first!";
            //    return;
            //}
            if (Session["ACL"].ToString().IndexOf("SA9") < 0)
            {
                lblPayError.Text = "Unauthorized!";
                return;
            }
            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "bll_bk_pay";
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", Convert.ToInt64(hidPayOrderNo.Value));
                para = cmd.Parameters.AddWithValue("@currency", hidCurrency.Value);
                para = cmd.Parameters.AddWithValue("@total_paid", lblTotal.Text);
                //para = cmd.Parameters.AddWithValue("@pay_type", lstPayType.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@pay_type", "F");//仅支持在线支付，中立结算，保障服务款
                //para = cmd.Parameters.AddWithValue("@bank", lstCardCompany.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@card_no", txtCardNo.Text);
                //para = cmd.Parameters.AddWithValue("@card_owner", txtCardOwner.Text);
                //para = cmd.Parameters.AddWithValue("@pay_id", -1);
                //para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                para = cmd.Parameters.AddWithValue("@ret_msg", SqlDbType.Char);
                para.Direction = ParameterDirection.Output;
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblPayError.Text = "Done. You can print receipt(s) now.";
                        //btnReceipts.Enabled = true;
                        //Response.Redirect("order_tickets.aspx?pnr=" + lblOrderNo.Text);
                        break;
                    case 1:
                        lblPayError.Text = "1:Updating trip ROE failed!";
                        break;
                    case 2:
                        lblPayError.Text = "2:Generate ticket EMD failed: " + Convert.ToSingle(para.Value).ToString();
                        break;
                    case 3:
                        lblPayError.Text = "3:Target not found!";
                        break;
                    case 20:
                        lblPayError.Text = "Service trip not ready! -" + Convert.ToSingle(para.Value).ToString() + ", please remove the service!";
                        break;
                    case 21:
                        lblPayError.Text = "Service is unavailiable by now! -" + Convert.ToSingle(para.Value).ToString() + ", please remove the service!";
                        break;
                    case 22:
                        lblPayError.Text = "Service city code not found! -" + Convert.ToSingle(para.Value).ToString() + ", please remove the service!";
                        break;
                    case 23:
                        lblPayError.Text = "Service is invalid! -" + Convert.ToSingle(para.Value).ToString() + ", please remove the service!";
                        break;
                    case 24:
                        lblPayError.Text = "Can not convert to local time, Service is invalid! -" + Convert.ToSingle(para.Value).ToString() + ", please remove the service!";
                        break;
                    case 100:
                        lblPayError.Text = "100:Target currency not found!";
                        break;
                    case 101:
                        lblPayError.Text = "101:Not sell office, or improper order status !";
                        break;
                    case 102:
                        lblPayError.Text = "102:Updating order payment failed!";
                        break;
                    case 103:
                        lblPayError.Text = "103:Passenger mismatched, generate E-tkt failed!";
                        break;
                    case 104:
                        lblPayError.Text = "104:Updating trip fare failed!";
                        break;
                    case 105:
                        lblPayError.Text = "105:Passengers and trips mismatched, generate E-tkt trips failed!";
                        break;
                    case 106:
                        lblPayError.Text = "106:Updating expiry failed!";
                        break;
                    case 108:
                        lblPayError.Text = "108:Pax list lost! " + Convert.ToSingle(para.Value).ToString();
                        break;
                    case 109:
                        lblPayError.Text = "109:Infant count mismatch adult's who hold infant! " + Convert.ToSingle(para.Value).ToString();
                        break;
                    case 107:
                        lblPayError.Text = "107:Total fare mismatch! " + Convert.ToSingle(para.Value).ToString();
                        break;
                    case 110:
                        lblPayError.Text = "110:Office not found or not valid!";
                        break;
                    case 111:
                        lblPayError.Text = "111:Deposit exchange rate not found!";
                        break;
                    case 112:
                        lblPayError.Text = "112:Lack of quota!";
                        break;
                    case 113:
                        lblPayError.Text = "113:Deducting deposit failed!";
                        break;
                    case 114:
                        lblPayError.Text = "114:Update flight cabin failed!";
                        break;
                    case 115:
                        lblPayError.Text = "115:Update flight class failed!";
                        break;
                    case 116:
                        lblPayError.Text = "116:Not entire open trip(s)!";
                        break;
                    case 117:
                        lblPayError.Text = "117:Seats are not OK, can not pay!";
                        break;
                    default:
                        lblPayError.Text = "Unknow error:" + ret_code.ToString();
                        break;
                }
                return;
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblPayError.Text = ex.Message;
                else
                    lblPayError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                return;
            }
        }
        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA3") < 0)
            {
                lblPayError.Text = "Unauthorized!";
                return;
            }

            int ret_code;
            Single total;
            SqlCommand cmd = new SqlCommand();
            hidCurrency.Value = lstCurrency.SelectedValue;
            try
            {
                cmd.CommandText = "bll_bk_calculate_total_price";
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidPayOrderNo.Value);
                //para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());//应该是销售单位的兑换表，而非当前用户单位的。不过支付对象应该是销售单位自身！
                para = cmd.Parameters.AddWithValue("@target_currency", lstCurrency.SelectedValue);
                para = cmd.Parameters.Add("@total_fare", SqlDbType.Money);
                para.Direction = ParameterDirection.Output;

                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        total = Convert.ToSingle(para.Value);
                        lblTotal.Text = total.ToString();
                        lblPayError.Text = "";
                        //GridView1.DataBind();
                        break;
                    case 203:
                        lblTotal.Text = "?";
                        lblPayError.Text = "203:Total fare is lower than 0! Check exchange rate.";
                        break;
                    case 204:
                        lblTotal.Text = "?";
                        lblPayError.Text = "204:Target currency not found!";
                        break;
                    default:
                        lblTotal.Text = "?";
                        lblPayError.Text = "Unknow error=" + ret_code.ToString();
                        break;
                }

            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblPayError.Text = ex.Message;
                else
                    lblPayError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnPayOnline_Click(object sender, EventArgs e)
        {
            //在线支付，交易额立即转入本公司账户
            //if (Session["ACL"].ToString().IndexOf("BD4") < 0)
            //{
            //    lblPayError.Text = "Unauthorized!";
            //    return;
            //}
            //try
            //{
            //    Convert.ToDouble(lblTotal.Text);
            //}
            //catch(Exception ex)
            //{
            //    lblPayError.Text = "Please calculate total fare first!";
            //    return;
            //}

            //StringBuilder str = new StringBuilder();
            //str.Append("?order=" + hidPayOrderNo.Value);
            ////str.Append("?gateway=" + lstGateways.SelectedValue.Trim());
            //str.Append("&request_type=P");//支付
            //str.Append("&currency=" + hidCurrency.Value.Trim());
            //str.Append("&amount=" + lblTotal.Text);
            //str.Append("&uid=" + Session["USER"].ToString());//!!!!
            ////str.Append("&callback=" + Application["http"].ToString() + "/pay_callback.aspx");
            ////string domain = TinyDAL.GetRequestDomain(HttpContext.Current.Request.Url.OriginalString);
            //str.Append("&callback=http://" + HttpContext.Current.Request.Url.Host + ":" + Convert.ToInt32(Application["HTTP_PORT"].ToString()).ToString() + Application["WEB_PATH"].ToString() + "/pay_callback.aspx");
            //str.Append("&attach=");//系统时间：精确到秒，作为主键之一？
            ////str.Append("&ip=");
            //string query = str.ToString();
            //str.Append("&pwd=" + Session["PWD"].ToString());
            //string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str.ToString(), "SHA1");
            ////Response.Redirect(Application["pay_agent"].ToString() + query + "&sign=" + sign);
            //Response.Write("<script language=\"javascript\">window.open('"+Application["pay_agent"].ToString()+query+"&sign=" + sign+"');</script>");
            //Response.End();
        }
        protected void btnRemoveFromCart_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["emdKEY"] == null)
            {
                lblError.Text = "Please select the ticket.";
                return;
            }
            key = Request.Form["emdKEY"];
            strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_bk_remove_emd");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNoEmd.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
                para = cmd.Parameters.AddWithValue("@cart_time", strArray[2]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                //para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                int ret_code;
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    grdEmd.DataBind();
                    lblEmdError.Text = "Done.";
                }
                else if (ret_code == 1)
                    lblEmdError.Text = "Failed to remove!";
                else if (ret_code == 2)
                    lblEmdError.Text = "Check order status!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void BtnAddToCart_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_bk_add_emd");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNoEmd.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", lstPaxSn.SelectedValue);
                para = cmd.Parameters.AddWithValue("@trip_sn", lstTripSn.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@airline", lstAirlines.SelectedValue);
                para = cmd.Parameters.AddWithValue("@emd_id", lstEmdID.SelectedValue);
                para = cmd.Parameters.AddWithValue("@quantity", txtQuantity.Text);
                para = cmd.Parameters.AddWithValue("@remark", txtEmdRemark.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                //para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                int ret_code;
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    grdEmd.DataBind();
                    lblEmdError.Text = "Done.";
                }
                else if (ret_code == 1)
                    lblEmdError.Text = "Order is not found or cancelled!";
                else if (ret_code == 2)
                    lblEmdError.Text = "Passenger is not found or cancelled!";
                else if (ret_code == 3)
                    lblEmdError.Text = "Invalid Service!";
                else if (ret_code == 4)
                    lblEmdError.Text = "Adding failed!";
                else if (ret_code == 5)
                    lblEmdError.Text = "Can not add EMD to 'OPEN' trip!";
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void lstAirlines_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void lstEmdID_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (Session["ACL"].ToString().IndexOf("BA2") < 0)
            //{
            //    lblEmdError.Text = "Unauthorized!";
            //    return;
            //}
            //try
            //{
            //    SqlCommand cmd = new SqlCommand("usp_get_emd_fare_by_id");
            //    SqlParameter para = cmd.Parameters.AddWithValue("@airline", lstAirlines.SelectedValue);
            //    para = cmd.Parameters.AddWithValue("@emd_id", lstEmdID.SelectedValue);
            //    para = cmd.Parameters.AddWithValue("@lang", Session["LANG"].ToString());
            //    DataTable tabRet = new DataTable();
            //    int ret_code;
            //    ret_code = TinyDAL.Exec(ref cmd, ref tabRet);
            //    if (ret_code == 0)
            //    {
            //        if (tabRet.Rows.Count == 1)
            //        {
            //            lblCurrency.Text = tabRet.Rows[0]["currency"].ToString();
            //            lblPrice.Text = tabRet.Rows[0]["price"].ToString();
            //            txtEmdNote.Text = tabRet.Rows[0]["emd_note"].ToString();
            //            //lstPayCurrencies_SelectedIndexChanged(sender, e);
            //        }
            //        else
            //        {
            //            lblCurrency.Text = "";
            //            lblPrice.Text = "";
            //            txtEmdNote.Text = "";
            //            lblEmdError.Text = "Record not found!";
            //        }
            //    }
            //    else
            //        lblEmdError.Text = "Error:" + ret_code.ToString();
            //}
            //catch (Exception ex)
            //{
            //    if (Application["DEBUG"].ToString() == "1")
            //        lblEmdError.Text = ex.Message;
            //    else
            //        lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            //}
        }
        //protected void lstAirlines_DataBound(object sender, EventArgs e)
        //{
        //    lstEmdID.DataBind();
        //}
        //protected void lstEmdID_DataBound(object sender, EventArgs e)
        //{
        //    lstEmdID_SelectedIndexChanged(sender, e);
        //}
        protected void btnRetreiveMerchan_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblEmdError.Text = "Unauthorized!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_bk_get_emd_detail_by_trip");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@trip_sn", lstTripSn.SelectedValue);
                para = cmd.Parameters.AddWithValue("@emd_id", lstEmdID.SelectedValue);
                para = cmd.Parameters.AddWithValue("@lang", Session["LANG"].ToString());
                DataTable tabRet = new DataTable();
                int ret_code;
                ret_code = TinyDAL.Exec(ref cmd, ref tabRet);
                if (ret_code == 0)
                {
                    if (tabRet.Rows.Count == 1)
                    {
                        lblCurrency.Text = tabRet.Rows[0]["currency"].ToString();
                        lblPrice.Text = tabRet.Rows[0]["price"].ToString();
                        txtEmdNote.Text = tabRet.Rows[0]["emd_note"].ToString().Replace("<br/>", "\n");//控件中出现<br/>会报错！
                        lblUnit.Text = tabRet.Rows[0]["unit"].ToString();
                    }
                    else
                    {
                        lblCurrency.Text = "";
                        lblPrice.Text = "";
                        txtEmdNote.Text = "";
                        lblUnit.Text = "";
                        lblEmdError.Text = "Record not found!";
                    }
                }
                else
                    lblEmdError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblEmdError.Text = ex.Message;
                else
                    lblEmdError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void lstTripSn_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstEmdID.DataBind();
        }
        protected void lstTripSn_DataBound(object sender, EventArgs e)
        {
            lstEmdID.DataBind();
        }
        protected void lstLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnReceipts.Attributes.Clear();
            btnReceipts.Attributes.Add("onclick", "javascript:window.open('Itinerary.aspx?bk_id=" + hidOrderNo.Value + "&lang=" + lstLang.SelectedValue + "');");
        }
        protected void lstLang_DataBound(object sender, EventArgs e)
        {
            lstLang.SelectedValue = Session["LANG"].ToString();
        }

        protected void lstCabin_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClass.Items.Clear();
            lstClass.DataBind();
            if (lstCabin.SelectedValue == "*")
            {
                lstClass.Items.Add(new ListItem("* - Default", "*"));
                lstClass.SelectedValue = "*";
            }
        }
        protected void lstAction_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (Request.Form["KEY"] == null)
            //{
            //    lblErrorK.Text = "Please select the trip first!";
            //    lstAction.SelectedValue = "S";
            //    return;
            //}

            if (lstAction.SelectedValue == "S")
                return;

            lstCabin.Items.Clear();
            lstCabin.DataBind();
            lstCabin.Items.Add(new ListItem("* - Default", "*"));
            lstCabin.SelectedValue = "*";
            lstCabin_SelectedIndexChanged(sender, e);
        }
    }
}